/**
 * Created by aa on 2016/9/8.
 */
'use strict';
var React = require('react');
var reactDom = require('react-dom/server');
var reactRedux = require('react-redux');
var configureStore = require('../public/javascripts/stores/index.js');
var HkfsViewPoint = require('../public/javascripts/containers/pc/HkfsViewPoint.jsx');
var Provider = reactRedux.Provider;
const store = configureStore();
var ModelRender = require('./modelRender.jsx');
var actions = require('../public/javascripts/actions/viewPoint');
var async = require('async');
var HttpOperate = require('../nodeLibrary/HttpOperate');
var backEndUrl=require('../nodeConfig/environmentConfig');
var httpOperateForHK = new HttpOperate(backEndUrl.hkBackEndUrl);
var newsContructer=require('../nodeLibrary/newsContructer');

exports.RenderFunc = (renderCallBack)=> {

    async.parallel({
        //财富报告
        getWealthReport: (callBack)=> {
           // let result = JSON.parse('{     "resultCode": 0,     "msg": "成功",     "size": 1000,     "result": [{         "id": "1",         "channel": "12",         "title": "xx分公司成立",         "type": "1",         "picAddress": "aa/aa/aa",         "url": "http://59.107.26.180/showdoc/index.php?s=/home/page/edit/page_id/92",         "status": "1",         "summary": "恭喜河南分公司成立",         "contentStr": "",         "cts": "123456789",         "ctsStr": "2016-08-09",         "ctsLongStr": "2016-08-09 12：10:10"     }, {         "id": "1",         "channel": "12",         "title": "xx分公司成立",         "type": "1",         "picAddress": "aa/aa/aa",         "url": "http://59.107.26.180/showdoc/index.php?s=/home/page/edit/page_id/92",         "status": "1",         "summary": "恭喜河南分公司成立",         "contentStr": "",         "cts": "123456789",         "ctsStr": "2016-08-09",         "ctsLongStr": "2016-08-09 12：10:10"     }] }');

            httpOperateForHK.httpGet('/api/article/type/WEALTH_REPORT?channel=0&curPage=0&pageSize=3', (result)=> {
                if (result.resultCode == '200') {
                    let wealthReport = newsContructer(result,3,1);
                    store.dispatch(actions.receiveWealthReport(wealthReport));
                }
                callBack();
            });

        },
        //焦点透视
        getPointItems: (callBack)=> {
            httpOperateForHK.httpGet('/api/article/type/FOCUS_VIEW?channel=0&curPage=0&pageSize=3', (result)=> {
                if (result.resultCode == '200') {
                    let pointItems = newsContructer(result, 3,1);
                    store.dispatch(actions.receivePointItems(pointItems));
                }
                callBack();
            });
        },
        //财富学堂
        getWealthClass: (callBack)=> {
           // let result = JSON.parse('{     "resultCode": 0,     "msg": "成功",     "size": 1000,     "result": [{         "id": "1",         "channel": "12",         "title": "xx分公司成立",         "type": "1",         "picAddress": "aa/aa/aa",         "url": "http://59.107.26.180/showdoc/index.php?s=/home/page/edit/page_id/92",         "status": "1",         "summary": "恭喜河南分公司成立",         "contentStr": "",         "cts": "123456789",         "ctsStr": "2016-08-09",         "ctsLongStr": "2016-08-09 12：10:10"     }, {         "id": "1",         "channel": "12",         "title": "xx分公司成立",         "type": "1",         "picAddress": "aa/aa/aa",         "url": "http://59.107.26.180/showdoc/index.php?s=/home/page/edit/page_id/92",         "status": "1",         "summary": "恭喜河南分公司成立",         "contentStr": "",         "cts": "123456789",         "ctsStr": "2016-08-09",         "ctsLongStr": "2016-08-09 12：10:10"     }] }');
            httpOperateForHK.httpGet('/api/article/type/WEALTH_SCHOOL?channel=0&curPage=0&pageSize=10',(result)=> {
                if (result.resultCode == '200') {
                    let wealthClass = {page: 1, totalPage:  Math.ceil(result.size/10), content: []};
                    for (var i = 0; i < result.result.length; i++) {
                        var resultItem = result.result[i];
                        var contentItem = {
                            reHref: '/hkfsDetail?id='+resultItem.id,
                            leftTitle: resultItem.title,
                            rightTitle: resultItem.ctsStr
                        };
                        wealthClass.content.push(contentItem);
                    }
                    /*let wealthClass = JSON.parse('{"page":1,"totalPage":10,"content":[{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数1","rightTitle":"2016-05-11"},{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数12","rightTitle":"2016-05-11"},{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数13","rightTitle":"2016-05-11"},{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数13","rightTitle":"2016-05-11"},{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数12","rightTitle":"2016-05-11"},{"reHref":"/dsf/sdf/sdf","leftTitle":"测试测试长度分数分数分数12","rightTitle":"2016-05-11"}]}');*/
                    store.dispatch(actions.receiveWealthClas(wealthClass));
                }
                callBack();
            });


        },
        //华康视点
        getViewPoints: (callBack)=> {
            //无视频服务器，暂时屏蔽
           /* httpOperateForHK.httpGet('/api/facade/company/weeklyView',(result)=> {
                let viewPoints;
                if (result.resultCode == '200') {
                    viewPoints=result.result;
                    store.dispatch(actions.receiveViewPoints(viewPoints));
                }
                callBack();
            });*/
            let viewPoints;
            callBack();
        }
    }, (err, results)=> {
        if (renderCallBack) {
            var modelRender = new ModelRender();
            var main = reactDom.renderToString(
                <Provider store={store}>
                    <HkfsViewPoint/>
                </Provider>
            );
            var initializeStore = JSON.stringify(store.getState());
            renderCallBack(modelRender.headerRender(), modelRender.barRender(), modelRender.footerRender(), main, initializeStore);
        }
    });

};
